<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/2
 * Time: 18:28
 */

namespace app\admin\model;


use think\Db;

class Union extends DataBase
{
    protected $name = 'union';
    protected $autoWriteTimestamp = true;

    public function indexData()
    {
        $page_config = [
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        ];

        $data = $this
            ->alias('a')
            ->field('a.*,b.u_id,c.real_name,c.card_id,c.phone,c.type')
            ->join('ss_users b','a.user_id = b.id','left')
            ->join('ss_union_info c','a.id = c.union_id','left')
//            ->where(['a.status'=>1,'a.type'=>1])
            ->paginate($page_config);
        return $data;
    }
    //同意开通家族
    public function unionAgree()
    {

        $union_id  = input('union_id');
        $re = $this->where('id',$union_id)->find();
        $ree = UnionMembers::where('user_id',$re['user_id'])->find();
        if($ree) return ['code'=>-100,'msg'=>'已经是其他家族成员'];
        if($re['state'] ==  1) return ['code'=>100,'msg'=>'审核成功'];
        Db::connect('db_data')->startTrans();
        try{
            $this->save(['state'=>1],['id'=>$union_id]);
            UnionMembers::create(['user_id'=>$re['user_id'],'union_id'=>$union_id,'type'=>1,'create_time'=>time()]);
            Db::connect('db_data')->commit();
            return ['code'=>100,'msg'=>'审核成功'];
        }catch (\Exception $e){
            Db::connect('db_data')->rollback();
            return ['code'=>100,'msg'=>'审核失败'];
        }
    }
    //拒绝开通家族
    public function unionRefuse()
    {
        $union_id  = input('union_id');
        $res = $this->save(['state'=>3],['id'=>$union_id]);
        if($res) return ['code'=>100,'msg'=>'拒绝成功'];
        else return ['code'=>-100,'msg'=>'拒绝失败'];
    }
}